package com.gateway.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gateway.common.core.page.PageResult;
import com.gateway.common.enums.ResultEnum;
import com.gateway.common.exception.BusinessException;
import com.gateway.common.utils.AjaxResult;
import com.gateway.common.utils.StringUtils;
import com.gateway.config.superclass.SuperService;
import com.gateway.system.domain.dto.FilterUrlDto;
import com.gateway.system.domain.entity.FilterUrl;
import com.gateway.system.domain.vo.FilterUrlVo;
import com.gateway.system.mapper.FilterUrlMapper;
import com.gateway.system.service.FilterUrlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;

/**
 * @author Huangkai
 */
@Slf4j
@Service
public class FilterUrlServiceImpl extends SuperService<FilterUrlMapper, FilterUrl> implements FilterUrlService {

    @Autowired
    FilterUrlMapper filterUrlMapper;

    @Autowired
    FilterUrlService filterUrlService;

    @Override
    public FilterUrlDto selectFilterUrlByUrl(String url) {
        LambdaQueryWrapper<FilterUrl> wrapper = new QueryWrapper<FilterUrl>().lambda();
        wrapper.eq(FilterUrl::getFilterUrl, url);
        FilterUrl filterUrl = filterUrlMapper.selectOne(wrapper);
        FilterUrlDto filterUrlDto = null;
        if(filterUrl != null){
            Mapper mapper = new DozerBeanMapper();
            filterUrlDto = mapper.map(filterUrl, FilterUrlDto.class);
        }
        return filterUrlDto;
    }

    @Override
    public AjaxResult selectFilterUrlList(FilterUrlVo filterUrlVo) {
        LambdaQueryWrapper<FilterUrl> wrapper = new QueryWrapper<FilterUrl>().lambda();
        IPage<FilterUrl> filterUrlIPage = filterUrlMapper.selectPage(new Page<>(filterUrlVo.getPageNum(), filterUrlVo.getPageSize()), wrapper);
        Page<FilterUrl> page = new PageResult().pageData(filterUrlIPage);
        return AjaxResult.ResultWithDataSuccess(page);
    }

    @Override
    public AjaxResult insertFilterUrl(FilterUrlVo filterUrlVo) {
        FilterUrlDto filterUrlDto = filterUrlService.selectFilterUrlByUrl(filterUrlVo.getFilterUrl());
        if(filterUrlDto != null){
            throw new BusinessException(ResultEnum.DATA_EXIST);
        }
        FilterUrl filterUrl = new FilterUrl();
        filterUrl.setUuid(StringUtils.getSnowFlakeId());
        filterUrl.setFilterUrl(filterUrlVo.getFilterUrl());
        filterUrl.setUrlDescribe(filterUrlVo.getUrlDescribe());
        filterUrl.setIsAuth(filterUrlVo.getIsAuth());
        filterUrl.insert();
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateFilterUrl(FilterUrlVo filterUrlVo) {
        FilterUrlDto filterUrlDto = filterUrlService.selectFilterUrlByUrl(filterUrlVo.getFilterUrl());
        if(filterUrlDto != null){
            if(!filterUrlDto.getUuid().equals(filterUrlVo.getUuid())){
                throw new BusinessException(ResultEnum.DATA_EXIST);
            }
        }
        LambdaUpdateWrapper<FilterUrl> wrapper = new UpdateWrapper<FilterUrl>().lambda();
        wrapper.eq(FilterUrl::getUuid, filterUrlVo.getUuid());
        Mapper mapper = new DozerBeanMapper();
        FilterUrl filterUrl = mapper.map(filterUrlVo, FilterUrl.class);
        filterUrl.update(wrapper);
        return AjaxResult.success();

    }

    @Override
    @CacheEvict()
    public AjaxResult deleteFilterUrl(FilterUrlVo filterUrlVo) {
        LambdaQueryWrapper<FilterUrl> wrapper = new QueryWrapper<FilterUrl>().lambda();
        wrapper.eq(FilterUrl::getUuid, filterUrlVo.getUuid());
        filterUrlMapper.delete(wrapper);
        return AjaxResult.success();
    }
}
